package com.bw.mallshop.mvvm.model;

import com.bw.common.BaseApplication;
import com.bw.common.entity.BaseEntity;
import com.bw.common.mvvm.model.BaseModel;
import com.bw.common.network.HttpType;
import com.bw.common.network.RetrofitFactory;
import com.bw.mallshop.App;
import com.bw.mallshop.dao.MaybeCarFunction;
import com.bw.mallshop.entity.CarListEntity;
import com.bw.mallshop.entity.DelCarEntity;
import com.bw.mallshop.entity.DelLocalCarEntity;
import com.bw.mallshop.entity.SubCarEntity;
import com.bw.mallshop.repository.ApiService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.Maybe;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.annotations.NonNull;

/**
 * 购物车列表界面
 * */
public class CarListModel extends BaseModel {
    //删除
    public Observable<BaseEntity>requestDel(Map<String,Object>parames){
        Observable<DelCarEntity>observable = RetrofitFactory.factory(HttpType.TOKEN)
                .createRetrofit().create(ApiService.class)
                .requestDelCar(createBody(parames));
        return changeObservable(observable);
    }
    //获取购物车列表
    public Observable<BaseEntity>requestCarList(Map<String,Object>parames){
        Observable<CarListEntity>observable = RetrofitFactory.factory(HttpType.TOKENSIGN)
                .createRetrofit().create(ApiService.class)
                .requestCarList(createBody(parames));
        return changeObservable(observable);
    }
    //提交
    public Observable<BaseEntity>requstSubCar(Map<String,Object>parames){
        Observable<SubCarEntity>observable = RetrofitFactory.factory(HttpType.TOKEN)
                .createRetrofit().create(ApiService.class)
                .requestSubCar(createBody(parames));
        return changeObservable(observable);
    }
    //获取离线购物车数据
    public Observable<BaseEntity>selCarDao(){
        App app = (App) BaseApplication.getInstance();
        Maybe<List<CarListEntity.Values>> maybe =  app.db.carDao().queryCarList();
        Observable<CarListEntity>observable = maybe.flattenAsObservable(new MaybeCarFunction());
        return changeObservable(observable);
    }
    //批量添加购物车数据源
    public Observable<BaseEntity>addAllCar(Map<String,Object>...parames){
        Observable<BaseEntity>[]list = new Observable[parames.length];
        for (int i = 0;i < parames.length;i ++){
            list[i]=changeObservable(RetrofitFactory.factory(HttpType.TOKEN).createRetrofit()
                    .create(ApiService.class).requestAddCar(createBody(parames[i])));
        }
        return mergeObservable(list);
    }
    //清空本地购物车数据
    public Observable<BaseEntity>delCarList(){
        Observable<DelLocalCarEntity>observable = Observable.create(new ObservableOnSubscribe<DelLocalCarEntity>() {
            @Override
            public void subscribe(@NonNull ObservableEmitter<DelLocalCarEntity> emitter) throws Exception {
                DelLocalCarEntity entity = new DelLocalCarEntity();
                entity.status = 0;
                entity.message = "本地数据清除成功";
                App app = (App) BaseApplication.getInstance();
                app.db.carDao().delete();
                emitter.onNext(entity);
                emitter.onComplete();
            }
        });
        return changeObservable(observable);
    }
}
